<template>
  <div class="addresslist">
    <van-nav-bar
      title="我的收获地址"
      left-text="返回"
      right-text="新增地址"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <van-address-list v-model="chosenAddressId" :list="res" @edit="onEdit" />
  </div>
</template>

<script>
import { address_list } from "../../api";
export default {
  data() {
    return {
      id: 0,
      chosenAddressId: "1",
      list: [],
      res: [],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.push("/home/mine");
    },
    onClickRight() {
      this.$router.push("/address");
      console.log("新增地址");
    },
    onAdd() {
      Toast("新增地址");
    },
    onEdit(item, index) {
      this.$router.push({
        name: "addressEdit",
        query: item,
      });
    },
  },
  mounted() {
    this.id = window.localStorage.getItem("userid");
    address_list({ userid: this.id }).then((res) => {
      this.list = res.data.list;
      // 将对象里的phone改为tel
      this.res = this.list.map((v) => {
        return {
          address: v.address,
          id: v.id,
          name: v.name,
          tel: v.phone,
          userid: v.userid,
        };
      });
      console.log(this.res);
    });
  },
};
</script>

<style>
</style>